package com.example.daxiguaforharmony.game.core.game;

import com.example.daxiguaforharmony.game.GameScene;
import com.example.daxiguaforharmony.game.core.base.DisplayObject;
import com.example.daxiguaforharmony.game.core.base.DisplayObjectContainer;

public class GameObject {
    public boolean enabled = true;
    protected GameScene _scene = null;
    protected DisplayObject _displayObject = null;
    protected int _tag = 0;

    public GameObject(GameScene scene, DisplayObject displayObject) {
        assert scene != null;
        this._scene = scene;
        this.replaceDisplayObject(displayObject);
    }

    public void setPosition(float x, float y) {
        this._displayObject.x = x;
        this._displayObject.y = y;
    }

    public void setPositionX(float x) {
        this._displayObject.x = x;
    }

    public void setTag(int tag) {
        this._tag = tag;
    }

    public int getTag() {
        return this._tag;
    }

    public void update(float dt) {
    }

    public void replaceDisplayObject(DisplayObject displayObject) {
        if (this._displayObject != null) {
            this._displayObject.removeFromParent();
            this._displayObject = null;
        }
        assert displayObject != null;
        this._displayObject = displayObject;
        float scale = this._scene.getScaleFactor();
        this._displayObject.scaleX = scale;
        this._displayObject.scaleY = scale;
    }

    public DisplayObject getDisplayObject() {
        return this._displayObject;
    }

    public void addToScene() {
        DisplayObjectContainer container = this._scene.getDisplayContainer();
        container.addChild(this._displayObject);
        this._scene.addGameObject(this);
    }

    public void removeFromScene() {
        this._scene.removeGameObject(this);
        this._displayObject.removeFromParent();
    }

    public void Destory() {
        if (this._displayObject != null) {
            this._displayObject.removeFromParent();
            this._displayObject = null;
        }
        this.removeFromScene();
    }
}
